﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Commands;
using Prism.Mvvm;
using System.Windows.Media;
using CPlatePrintSys.Annotation.Events;
using CPlatePrintSys.Annotation.Models;
using MahApps.Metro.Controls;
using Prism.Events;

namespace CPlatePrintSys.Win.ViewModels
{

    public class ProcessLayer : BindableBase
    {
        private string _name;
        private Color _color;
        private bool _isSelected;
        private bool _isPartLayer = true;
        private bool _isAnnotationLayer;
        public DelegateCommand<ProcessLayer> ShowColorPickerCommand { get; }

        public DelegateCommand ConfirmColorCommand { get; }
        public DelegateCommand CancelColorCommand { get; }

        public string Name { get => _name; set => SetProperty(ref _name, value); }
        public Color Color { get => _color; set => SetProperty(ref _color, value); }

        public bool IsPartLayer { get => _isPartLayer; set => SetProperty(ref _isPartLayer, value); }
        public ObservableCollection<ProcessLayer> SubLayers { get; } = new ObservableCollection<ProcessLayer>();
        public ProcessLayer Parent { get; set; }
        public bool IsSelected { get => _isSelected; set => SetProperty(ref _isSelected, value); }

        public bool IsAnnotationLayer { get => _isAnnotationLayer; set => SetProperty(ref _isAnnotationLayer, value); }


        private ProcessLayer _tempLayer;
        private Color _originalColor;

        public ProcessLayer(string name = "", Color? color = null)
        {
            Name = name;
            Color = color ?? Colors.Black;
            SubLayers.CollectionChanged += SubLayers_CollectionChanged;

            ShowColorPickerCommand = new DelegateCommand<ProcessLayer>(ExecuteShowColorPicker);
            ConfirmColorCommand = new DelegateCommand(ExecuteConfirmColor);
            CancelColorCommand = new DelegateCommand(ExecuteCancelColor);
        }

        private void SubLayers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                IsPartLayer = false; // 如果有子图层，则不是叶子节点
            }
        }


        private void ExecuteShowColorPicker(ProcessLayer layer)
        {
            if (layer == null) return;

            _tempLayer = layer;
            _originalColor = layer.Color;

            // 获取Flyout控件并显示
            var flyout = System.Windows.Application.Current.MainWindow.FindName("ColorPickerFlyout") as MahApps.Metro.Controls.Flyout;
            if (flyout != null)
            {
                flyout.IsOpen = true;
            }
        }

        private void ExecuteConfirmColor()
        {
            // 获取Flyout控件并关闭
            var flyout = System.Windows.Application.Current.MainWindow.FindName("ColorPickerFlyout") as MahApps.Metro.Controls.Flyout;
            if (flyout != null)
            {
                flyout.IsOpen = false;
            }
            _tempLayer = null;
        }

        private void ExecuteCancelColor()
        {
            if (_tempLayer != null)
            {
                _tempLayer.Color = _originalColor;
            }

            // 获取Flyout控件并关闭
            var flyout = System.Windows.Application.Current.MainWindow.FindName("ColorPickerFlyout") as MahApps.Metro.Controls.Flyout;
            if (flyout != null)
            {
                flyout.IsOpen = false;
            }
            _tempLayer = null;
        }
    }


    public class ProcessSequenceLayerCtrlViewModel : BindableBase
    {
        private IEventAggregator _eventAggregator;
        private MetroWindow _metroWindow;

        public ObservableCollection<ProcessLayer> RootCollection { get; }
        public ProcessLayer RootLayer { get; }
        private ProcessLayer _selectedLayer;
        public ProcessLayer SelectedLayer
        {
            get => _selectedLayer;
            set
            {
                SetProperty(ref _selectedLayer, value);
            }
        }

        public DelegateCommand AddLayerCommand { get; }
        public DelegateCommand RemoveLayerCommand { get; }

        public ProcessSequenceLayerCtrlViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _metroWindow = System.Windows.Application.Current.MainWindow as MetroWindow;

            RootLayer = new ProcessLayer("全部");
            RootCollection = new ObservableCollection<ProcessLayer> { RootLayer };
            SelectedLayer = RootLayer; // 默认选中根节点

            AddLayerCommand = new DelegateCommand(ExecuteAddLayer);
            RemoveLayerCommand = new DelegateCommand(ExecuteRemoveLayer, CanRemoveLayer);

        }

       
        private void ExecuteAddLayer()
        {
            var parent = RootLayer;
            var newLayer = new ProcessLayer($"图层 {parent.SubLayers.Count + 1}");
            newLayer.IsAnnotationLayer = true;
            newLayer.IsPartLayer = false;
            newLayer.Parent = parent;
            parent.SubLayers.Add(newLayer);
        }

        private void ExecuteRemoveLayer()
        {
            if (SelectedLayer != null && SelectedLayer != RootLayer && SelectedLayer.Parent != null)
            {
                SelectedLayer.Parent.SubLayers.Remove(SelectedLayer);
                SelectedLayer = null;
            }
        }

        private bool CanRemoveLayer()
        {
            return true;
        }


    }
 
}
